<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
    <!--<el-form-item label="培训机构代码" prop="organizeid">-->
      <!--<el-input v-model="dataForm.organizeid" placeholder="培训机构代码"></el-input>-->
    <!--</el-form-item>-->
    <el-form-item label="校区名称" prop="shopname">
      <el-input v-model="dataForm.shopname" placeholder="店铺名称"></el-input>
    </el-form-item>
    <el-form-item label="校区详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="店铺详细地址"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="contact">
      <el-input v-model="dataForm.contact" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="负责人电话" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="负责人电话"></el-input>
    </el-form-item>
      <el-form-item label="顾问" prop="adviser">
        <el-input v-model="dataForm.adviser" placeholder="顾问"></el-input>
      </el-form-item>
      <el-form-item label="教练" prop="coach">
        <el-input v-model="dataForm.coach" placeholder="教练"></el-input>
      </el-form-item>
	  <el-form-item label="经度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="经度"></el-input>
      </el-form-item>
	  <el-form-item label="纬度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="纬度"></el-input>
      </el-form-item>
    <el-form-item label="提前预约限制(分钟)" prop="preLimit">
        <el-input v-model="dataForm.preLimit" placeholder="提前预约限制(分钟)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          organizeid: '',
          shopname: '',
          address: '',
          region: '',
          tel: '',
          coach: '',
          contact: '',
          adviser: '',
          latitude: '',
          longitude: '',
          createtime: '',
          updatetime: '',
          preLimit: 0
        },
        dataRule: {
          organizeid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          shopname: [
            { required: true, message: '店铺名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '店铺详细地址不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '店铺区域不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '店铺电话不能为空', trigger: 'blur' }
          ],
          // startbusinesstime: [
          //   { required: true, message: '开店时间不能为空', trigger: 'blur' }
          // ],
          contact: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          corporationmobile: [
            { required: true, message: '法人电话不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ]
          // createtime: [
          //   { required: true, message: '注册时间不能为空', trigger: 'blur' }
          // ],
          // updatetime: [
          //   { required: true, message: '修改时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/shop/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopname = data.shop.shopName
                this.dataForm.address = data.shop.address
                this.dataForm.contact = data.shop.contact
                this.dataForm.tel = data.shop.tel
                this.dataForm.adviser = data.shop.adviser
                this.dataForm.coach = data.shop.coach
                this.dataForm.latitude = data.shop.latitude
                this.dataForm.longitude = data.shop.longitude
                this.dataForm.preLimit = data.shop.preLimit
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/shop/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopName': this.dataForm.shopname,
                'address': this.dataForm.address,
                'region': this.dataForm.region,
                'tel': this.dataForm.tel,
                'startbusinesstime': this.dataForm.startbusinesstime,
                'corporation': this.dataForm.corporation,
                'corporationmobile': this.dataForm.corporationmobile,
                'createtime': this.dataForm.createtime,
                'updatetime': this.dataForm.updatetime,
                'adviser': this.dataForm.adviser,
                'coach': this.dataForm.coach,
                'latitude': this.dataForm.latitude,
                'longitude': this.dataForm.longitude,
                'preLimit': this.dataForm.preLimit
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
